package com.zlk.algorithm.algorithm.sort.basesSimp;

import com.zlk.algorithm.algorithm.sort.SortUtils;
import org.junit.Test;

import java.util.Arrays;

/**
 * @program: algorithm
 * @ClassName BubbleSort
 * @description: 冒泡排序
 * @author: slfang
 * @create: 2023-12-19 10:28
 * @Version 1.0
 **/
public class BubbleSort {

    @Test
    public void test(){
        int testTimes = 10000;
        int randomLength = 20;
        int randomVal = 100;

        for (int i = 0; i < testTimes; i++) {
            int[] randomArr1 = SortUtils.getRandomArr(randomVal, randomLength);
            int[] copy = SortUtils.copyArray(randomArr1);
            SortUtils.comparator(copy);
            test(randomArr1);
            if(!SortUtils.isEqual(copy,randomArr1)){
                System.out.println("第"+(i+1)+"测试失败"+Arrays.asList(copy));
            }

        }
        System.out.println("success !!!");
    }






    public void sort(int[] arr){
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < arr.length-i; j++) {
                if(arr[j]>arr[j+1]){
                    SortUtils.swap(arr,j,j+1);
                }
            }
        }
    }

    public void bubbleSort(int[] arr){
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = 1; i <arr.length ; i++) {
            for (int j = 0; j <arr.length-i ; j++) {
                if(arr[j]>arr[j+1]){
                    SortUtils.swap(arr,j,j+1);
                }
            }
        }
    }




    public void test(int[] arr){
        if (arr == null || arr.length < 2) {
            return;
        }
        int n = arr.length;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n-i; j++) {
                if(arr[j]>arr[j+1]){
                    SortUtils.swap(arr,j,j+1);
                }
            }
        }
    }








}
